package com.llynosy.interview.controller.api;

import com.llynosy.interview.base.BaseController;
import com.llynosy.interview.po.Answer;
import com.llynosy.interview.po.Result;
import com.llynosy.interview.po.StatusCode;
import com.llynosy.interview.service.AnswerService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/api/answer")
@RestController()
public class APIAnswerController extends BaseController<Answer> {
    @Override
    public Result addE(@RequestBody Answer answer) throws Exception {
        if(answer==null|| StringUtils.isEmpty(answer.getContent())){
            throw new RuntimeException("内容不可以为空");
        }
        if(answer==null|| answer.getProblemId()==null){
            throw new RuntimeException("问题ID不可以为空");
        }
        return super.addE(answer);
    }

    @GetMapping("/recommendAnswer/{problemId}")
    public Result findRecommendAnswer(@PathVariable("problemId") Long problemId){
        Answer answer =((AnswerService)(this.commCrudService)).getRecommendAnswer(problemId);
        return new Result(true, StatusCode.OK,"SUCCESS",answer);
    }

    @RequestMapping("/problem/{id}")
    public Result findAnswersByProblemId(@PathVariable("id")Long id){
        List<Answer> answers=((AnswerService)this.commCrudService).getAnswersByProblemId(id);
        return new Result(true, StatusCode.OK,"SUCCESS",answers);
    }

}
